package com.manage.project.biz.controller;

import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.*;
import com.manage.project.biz.service.*;
import com.manage.project.system.domain.SysNotice;
import com.manage.project.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
//import com.ruoyi.common.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专项检查评价Controller
 *
 * @author ycd
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/biz/specialevaluate")
public class CivilEnginneeringSpecialEvaluateController extends BaseController
{
    @Autowired
    private ICivilEnginneeringSpecialEvaluateService civilEnginneeringSpecialEvaluateService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;
    @Autowired
    private ICivilEnginneeringTreatmentService iCivilEnginneeringTreatmentService;
    @Autowired
    private ISysNoticeService noticeService;
    /**
     * 查询专项检查评价列表
     */
    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate)
    {
        startPage();
        List<CivilEnginneeringSpecialEvaluate> list = civilEnginneeringSpecialEvaluateService.selectCivilEnginneeringSpecialEvaluateList(civilEnginneeringSpecialEvaluate);
        return getDataTable(list);
    }

    /**
     * 导出专项检查评价列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:export')")
//    @Log(title = "专项检查评价", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate) throws IOException {
//        List<CivilEnginneeringSpecialEvaluate> list = civilEnginneeringSpecialEvaluateService.selectCivilEnginneeringSpecialEvaluateList(civilEnginneeringSpecialEvaluate);
//        ExcelUtil<CivilEnginneeringSpecialEvaluate> util = new ExcelUtil<CivilEnginneeringSpecialEvaluate>(CivilEnginneeringSpecialEvaluate.class);
//        util.exportExcel(response, list, "专项检查评价数据");
//    }

    @Log(title = "专项检查评价", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate) throws IOException {
        List<CivilEnginneeringSpecialEvaluate> list = civilEnginneeringSpecialEvaluateService.selectCivilEnginneeringSpecialEvaluateList(civilEnginneeringSpecialEvaluate);
        ExcelUtil<CivilEnginneeringSpecialEvaluate> util = new ExcelUtil<CivilEnginneeringSpecialEvaluate>(CivilEnginneeringSpecialEvaluate.class);
        return  util.exportExcel(list, "专项检查评价数据");
    }
    /**
     * 获取专项检查评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:query')")
    @GetMapping(value = "/{specialEvaluateId}")
    public AjaxResult getInfo(@PathVariable("specialEvaluateId") Long specialEvaluateId)
    {
        return AjaxResult.success(civilEnginneeringSpecialEvaluateService.selectCivilEnginneeringSpecialEvaluateBySpecialEvaluateId(specialEvaluateId));
    }

    /**
     * 新增专项检查评价
     */
    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:add')")
    @Log(title = "专项检查评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate)
    {
        String roadName=civilEnginneeringSpecialEvaluate.getRoadName();
        String tunnelName=civilEnginneeringSpecialEvaluate.getTunnelName();
        String tunnelId=civilEnginneeringSpecialEvaluate.getTunnelId();
        String facility=civilEnginneeringSpecialEvaluate.getFacilityName();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
//       校验设备设备接口
//        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.
//            return new AjaxResult().error(tunnelName+"设备未安装"+facility+",请联系管理员");
//        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringSpecialEvaluateService.insertCivilEnginneeringSpecialEvaluate(civilEnginneeringSpecialEvaluate));

        }
    }

    /**
     * 修改专项检查评价
     */
    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:edit')")
    @Log(title = "专项检查评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate)
    {
        Date time=civilEnginneeringSpecialEvaluate.getEvaluateDate();
        String roadId=civilEnginneeringSpecialEvaluate.getRoadId();
        String roadName=civilEnginneeringSpecialEvaluate.getRoadName();
        String tunnelName=civilEnginneeringSpecialEvaluate.getTunnelName();
        String tunnelId=civilEnginneeringSpecialEvaluate.getTunnelId();
        String facility=civilEnginneeringSpecialEvaluate.getFacilityName();
        Long positionNumber=civilEnginneeringSpecialEvaluate.getPositionNumber();
        String maintenanceOrganization=civilEnginneeringSpecialEvaluate.getMaintenanceOrganization();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
//       校验设备设备接口,里程桩号不在评价表中
//        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.
//            return new AjaxResult().error(tunnelName+"设备未安装"+facility+",请联系管理员");
//        }
////        修改专项检查评价中状况值不为0的进行自动生成病害处治
//        if(positionNumber!=0&&positionNumber!=null){
//            CivilEnginneeringTreatment civilEnginneeringTreatment= new CivilEnginneeringTreatment();
//            civilEnginneeringTreatment.setRoadId(roadId);
//            civilEnginneeringTreatment.setRoadName(roadName);
//            civilEnginneeringTreatment.setTunnelId(tunnelId);
//            civilEnginneeringTreatment.setTunnelName(tunnelName);
////            评价的状况值给到病害处治前状况值
//            civilEnginneeringTreatment.setLastTreatmentNumber(positionNumber);
//            civilEnginneeringTreatment.setMaintenanceOrganization(maintenanceOrganization);
//            iCivilEnginneeringTreatmentService.insertCivilEnginneeringTreatment(civilEnginneeringTreatment);
//        }
//        状况值不为0的进行自动生成病害处治通知公告
        if(positionNumber!=0){
            SysNotice sysNotice= new SysNotice();
            sysNotice.setNoticeTitle("土建结构-病害处治");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("专项检查评价提交："+roadId+roadName+"-"+tunnelId+tunnelName+"-"+facility+"状况值为"+positionNumber+"的处治任务");
            sysNotice.setCreateBy(getUsername());
            sysNotice.setCreateTime(time);
            noticeService.insertNotice(sysNotice);
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringSpecialEvaluateService.updateCivilEnginneeringSpecialEvaluate(civilEnginneeringSpecialEvaluate));
        }
    }

    /**
     * 删除专项检查评价
     */
    @PreAuthorize("@ss.hasPermi('biz:specialevaluate:remove')")
    @Log(title = "专项检查评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specialEvaluateIds}")
    public AjaxResult remove(@PathVariable Long[] specialEvaluateIds)
    {
        return toAjax(civilEnginneeringSpecialEvaluateService.deleteCivilEnginneeringSpecialEvaluateBySpecialEvaluateIds(specialEvaluateIds));
    }
}
